﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.General.BL
{
    public class ConfigRetrieval
    {
        private Session _sess = null;

        #region GetTableNames
        private DataTable GetTableNames()
        {
            string[] tables = null;
            DataTable TableNames = null;
            tables = SchemaRetrieval.GetDatabaseTables(this._sess);
            {
                TableNames = new DataTable();
                TableNames.Columns.Add("Identifier", typeof(string));
                TableNames.Columns.Add("Description", typeof(string));
                for (int i = 0; i < tables.Length; i++)
                {
                    TableNames.Rows.Add(new object[] {tables[i],tables[i]});
                }
            }
            return TableNames;
        }
        #endregion GetTableNames

        #region GetFieldNamesForTable
        /// <summary>
        /// This method will retrieve the field names for the specified tableName.
        /// </summary>
        public static DataTable GetFieldNamesForTable(Session session, string tableName)
        {
            // create a table to hold the field names and their translations
            DataTable translations = new DataTable();

            // retirieve the fieldnames
            DataTable fieldNames = SchemaRetrieval.GetFieldNamesForTable(session, tableName);

            // add a column to the new table for both the field name and its translation
            translations.Columns.AddRange(new DataColumn[]{
															  new DataColumn("FIELDNAME"), 
															  new DataColumn("TRANSLATION")
														  });

            if (!DataManager.IsEmpty(fieldNames))
            {
                // look through the field names and translate each
                foreach (DataRow row in fieldNames.Rows)
                {
                    string fieldName = DataManager.GetString(row, 0);
                    //string fieldNameNoUnderscore = StringManager.RemoveUnderScore(fieldName);

                    translations.Rows.Add(new object[] { fieldName, fieldName});
                }

                // return the names/translation table
            }

            return translations;
        }
        #endregion GetFieldNamesForTable

        #region GetGenericDropDownData
        /// <summary>
        /// Gets the Generic data in a dropdown.
        /// </summary>
        /// <param name="configType"></param>
        /// <param name="recordType"></param>
        /// <param name="sys1Value"></param>
        /// <returns></returns>
        public DataTable GetGenericDropDownData(string recordType, string sys1Value)
        {
            String sql = "SELECT IDENTIFIER,DESCRIPTION FROM GENERIC_CONFIG_DETAIL WHERE RECORD_TYPE = @KEY AND ACTIVE = @ACTIVE";

            using (DataHelper helper = new DataHelper(this._sess))
            {
                if (!StringManager.IsEmpty(sys1Value))
                {
                    sql += " AND SYS1VALUE = @SYS1VALUE";
                    return helper.GetTable(CommandType.Text, sql, helper.BuildParameter("@KEY", recordType),
                        helper.BuildParameter("@ACTIVE", "Y"),
                        helper.BuildParameter("@SYS1VALUE", sys1Value));
                }
                else
                {
                    return helper.GetTable(CommandType.Text, sql, helper.BuildParameter("@KEY", recordType),
                        helper.BuildParameter("@ACTIVE", "Y"));
                }
            }
        }
        #endregion GetGenericDropDownData
    }
}
